<?php 
		include_once "library/democrart/modules/product_model/vo/OrderModelQuadroOptions.class.php";
		if(FALSE){$HttpResult = new HttpResult();}
		
		$HttpContentResult = $HttpResult->getHttpContentResult();
	
//	Debug::print_r($HttpContentResult);	
	
	$head = new HtmlHeader(new HttpHeader());
		$head->addCSS('reset');
		$head->addCSS('print','', 'print');
		$head->addCSS('ie',  'css/ie', 'print',true);
		
		$head->addCSS('forum',"css/admin");		
		
		//<!-- PROGRAMACAO JS | JQUERY  -->
		$head->addJS(array('jquery', 'masked'));
		
    //MENU DROPDOWN
    $head->addJS('Menu principal','js/menu/ddsmoothmenu');
    $head->addCSS('Menu CSS','js/menu/ddsmoothmenu');
    
    //TINYMCE
    $head->addJS('tinymce','js/tinymce/jquery.tinymce');
    
    //FILEUPLOAD
    $head->addJS('so','js/uploadify/swfobject');
    $head->addJS('uploadify','js/uploadify/jquery.uploadify.v2.1.4.min');
    $head->addCSS('Upload CSS','js/uploadify/uploadify');
		       
		//SLIDER SHOW
		$head->addJS('cycle', 'js/cycle/jquery.cycle.min');
	
		//<!-- JQUERY UI  -->
		$head->addJS('UI', 'js/UI/js/jquery-ui-1.8.6.custom.min');
		$head->addCSS('UI','js/UI/css/smoothness/jquery-ui-1.8.6.custom');
		
     //SELECTMENU
    $head->addJS('selectmenu','js/UI/development-bundle/ui/jquery.ui.selectmenu');
    $head->addCSS('selectmenu CSS','js/UI/css/jquery.ui.selectmenu');
    
    $head->addCSS('adminmenu','css/lista_produtos_admin');
    $head->addCSS('adminprodutos','css/produtos');
    
		//<!-- VTIP  -->
		$head->addJS('vtip', 'js/vtip/vtip');
		$head->addCSS('vtip', 'js/vtip/vtip');
		
		//<!-- TABLE SORTER -->
		$head->addJS('tablesorter', 'js/tablesorter/tablesorter');
		$head->addCSS('tablesorter', 'js/tablesorter/tablesorter');
    
    //<!-- PROGRAMACAO JS | JQUERY  -->
    $head->addJS(array('admin','admin_select', 'admin/product/product'));
	
$header_html = $head->show(TRUE);
$header_html = str_replace(array("democrart", "democrArt", "Democrart", "DemocrArt"), array("expressaoartistica", "Expressão Artística", "Expressão Artística", "Expressão Artística"), $header_html);
echo $header_html;

function showOptionsAndSelectTheSelectableItem($array_itens, $id_selected = NULL){
	
	$text = "";
	foreach($array_itens as $itemStd){
		$selected = "";
		if($itemStd->id == $id_selected){
			//item selecionado
			$selected = " selected=\"selected\" ";
		}
		$text .= "
		<option value=\"$itemStd->id\" $selected >$itemStd->name</option>
		";
	}
	return $text;
}
?>
<body>
<?php include Config::getFolderView("parts/admin/header.php"); ?>
<div id="main">
	<form action="" method="post"  enctype="multipart/form-data">
	<?php if($HttpContentResult->product_model->product_model_democrart_quadro_id > 0){?>
		<input type="hidden" name="product_model_democrart_quadro_id" value="<?php echo $HttpContentResult->product_model->product_model_democrart_quadro_id;?>" />
	<?php } ?>
	
	<?php if($HttpContentResult->product_model->product_model_id > 0){?>
		<input type="hidden" name="product_model_id" value="<?php echo $HttpContentResult->product_model->product_model_id;?>" />
	<?php } ?>
    <div id="main_content" class="quadro-detail">
    	  		   
		  <div id="box_right">
		  		<!-- DETALHE -->
					
					<?php
//					echo "ococco";
					$HttpContentResult->product_model->getProduct();
//					Debug::Print_r($HttpContentResult);
						$ProductModel 		= $HttpContentResult->product_model;
						$path_img 			= $HttpContentResult->product_model->quadro_image_path;
						//$thumb 			= Config::getAsset("assets/$path_img");
						$artist_id 			= $HttpContentResult->product_model->quadro_artist_id;
						$product_model_id 	= $HttpContentResult->product_model->id;
						$thumb 				= Config::getRootPath("image/get_image/product_id.$product_model_id/max_width.130/max_height.130/");
						$thumbParede		= Config::getRootPath("image/get_image/product_id.$product_model_id/max_width.500/max_height.500/");
						//$img 				= Config::getAsset("assets/" . str_replace('/thumb','', $path_img));
						$img 				= Config::getRootPath("image/get_image/product_id.$product_model_id/md.1/max_width.130/max_height.130/");
						$img_indique 		= Config::getRootPath("image/get_image/product_id.$product_model_id/md.1/max_width.125/max_height.125/");
						$nameUrlArtist 		= $HttpContentResult->product_model->getQuadroArtistName();
						$lnArtist 			= Config::getRootPath("artist/detail/artista.$artist_id/$nameUrlArtist");
						$currency_url 		= urlencode("http://".DataHandler::removeDobleBars($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']));
						//só pra usar a mesma variavel que tem no admin
						$retornoDaPaginaHTML = $HttpContentResult;
						/**
						"id"
						"product_model_democrart_quadro_id"
						"product_type_id"
						"name"
						"description"
						"code"
						"year"
						"date_update"
						
						"quadro_composition_id"
						"quadro_technique_id"
						"quadro_format_id"
						"quadro_theme_id"
						"quadro_type_id"
						"quadro_style_id" 
						"quadro_artist_id" 
						"quadro_widht"
						"quadro_height"
						"quadro_note"
						"quadro_image_path"
						"highlight"
						 */
					 ?>
					 
					<?php 
					//se tiver id, manda o id tb
					if($ProductModel->product_model_democrart_quadro_id > 0){
						?>
						<input type="hidden" name="id" value="<?php echo $ProductModel->product_model_democrart_quadro_id;?>"></input>
						<?php
					}
					//verifica se foi mandado a 
					if($HttpContentResult->returnResult && !$HttpContentResult->returnResult->success){
						//se deu erro, mostra a array de erros
						foreach($HttpContentResult->returnResult->array_messages as $message){
							?>
							<p class="success"><?php echo $message;?></p>
							<?php 
						}
					}
					?>
					<div style="display:block;clear:both;">
						<?php
						if($HttpContentResult->product_model->quadro_image_path){
							?>
							<div class="detalheImagem">
								<img id="enviar_foto_image" src="<?php echo $thumb;?>" alt="Trocar/enviar imagem" title="<?php echo $HttpContentResult->product_model->name; ?>" class="imgObraDetalhe" />
								<span id="enviar_trocar_message" class="zoom ready" onclick="$('#enviar_foto_image').css('display', 'none');$(this).css('display', 'none');$('#enviar_foto').css('display', 'block');">Trocar/enviar imagem</span>
								<input id="enviar_foto" style="display: none;" name="Filedata" type="file" />
							</div>	
							<?php 
						} else {
							//nao tem imagem
							?>
							<div class="detalheImagem">
								Enviar imagem:<input id="enviar_foto" style="display: block;" name="Filedata" type="file" />
							</div>	
							<?php
						}
						?>
						<div id="check_destaque">
							<?php 
							$checked_destaque = ($ProductModel->highlight == 1)?" checked=\"checked\" ":"";
							?>
							<label><input type="checkbox" <?php echo $checked_destaque;?> name="highlight" value="1"></input>Este produto é destaque?</label>
						</div>					
						<div id="check_destaque">
							<?php 
							$checked_active = ($ProductModel->active == 1)?" checked=\"checked\" ":"";
							?>
							<label><input type="checkbox" <?php echo $checked_active;?> name="active" value="1"></input>Publicar Produto?</label>
						</div>					
						<div class="infoProduto">
							
								<br />
								<div>
									<label>Nome do quadro:</label> <input type="text" id="name" name="name" value="<?php echo $HttpContentResult->product_model->name; ?>"></input>
								</div>
								<br />
								<p>Para auto criar codigo, deixe vazio.</p>
								<br />
								<div>
									<label>Ítem n#: </label>
										<input type="text" id="code" name="code" value="<?php echo $HttpContentResult->product_model->code; ?>"></input>
								</div>
								<br />
								<div>
								<?php 		
								//criando o item vazio
								$optionSelecionaStd = new stdClass();
								$optionSelecionaStd->id 		= 0;
								$optionSelecionaStd->name 		= "selecione";
								$optionSelecionaStd->active 	= 1;
								$optionSelecionaStd->order 		= 0;
								$temp_array_composition 	= $retornoDaPaginaHTML->array_composition;
								$temp_array_composition[] 	= $optionSelecionaStd;
								?>
								
								<label>Artista:</label>
													<div class="ready" id="getQuadroArtistName()_show" onclick="$(this).css('display', 'none'); $('#quadro_artist_id').css('display', 'block');">
														<?php echo ($HttpContentResult->product_model->getQuadroArtistName() == "")?"não definido":$HttpContentResult->product_model->getQuadroArtistName(); ?>
													</div>
													<select id="quadro_artist_id" name="quadro_artist_id" style="display: none;">
														<?php
														echo showOptionsAndSelectTheSelectableItem($retornoDaPaginaHTML->array_artist, $artist_id);
														?>
													</select>
								</div>
								<br />
								<div>
									<label>Edição Limitada:</label>
													<div class="ready" id="quadro_type_button_show" onclick="$(this).css('display', 'none'); $('#quadro_type_id').css('display', 'block');"><?php echo ($HttpContentResult->product_model->getQuadroTypeName())?$HttpContentResult->product_model->getQuadroTypeName():"escolher"; ?></div>
													<select id="quadro_type_id" name="quadro_type_id" style="display: none;">
														<?php
														echo showOptionsAndSelectTheSelectableItem($retornoDaPaginaHTML->array_type, $ProductModel->quadro_type_id);
														?>
													</select>
								</div>
								<br />
								<label>Composição:</label>
													<div class="ready" id="quadro_composition_name_show" onclick="$(this).css('display', 'none'); $('#quadro_composition_id').css('display', 'block');">
														<?php echo ($HttpContentResult->product_model->getQuadroCompositionName() == "")?"não definida":$HttpContentResult->product_model->getQuadroCompositionName(); ?>
													</div>
													<select id="quadro_composition_id" name="quadro_composition_id" style="display: none;">
														<?php
														echo showOptionsAndSelectTheSelectableItem($temp_array_composition, $ProductModel->quadro_composition_id);
														?>
													</select>
								</div>
								<br />
								<div>
									<label>Tema: </label>
													<div class="ready" id="quadro_theme_name_show" onclick="$(this).css('display', 'none'); $('#quadro_theme_id').css('display', 'block');"><?php echo ($HttpContentResult->product_model->getQuadroThemeName())?$HttpContentResult->product_model->getQuadroThemeName():"escolher"; ?></div>
													<select id="quadro_theme_id" name="quadro_theme_id" style="display: none;">
														<?php
														echo showOptionsAndSelectTheSelectableItem($retornoDaPaginaHTML->array_theme, $ProductModel->quadro_theme_id);
														?>
													</select>
								</div>
								<br />
								<div>
									<label>Técnicas:</label>
													<div class="ready" id="quadro_type_button_show" onclick="$(this).css('display', 'none'); $('#quadro_technique_id').css('display', 'block');"><?php echo ($HttpContentResult->product_model->getQuadroTechniqueName())?$HttpContentResult->product_model->getQuadroTechniqueName():"escolher"; ?></div>
													<select id="quadro_technique_id" name="quadro_technique_id" style="display: none;">
														<?php 
														echo showOptionsAndSelectTheSelectableItem($retornoDaPaginaHTML->array_technique, $ProductModel->quadro_technique_id);
														?>
													</select>
								</div>
								<br />
								<div>
									<label>Formato: </label>
													<div class="ready" id="quadro_format_show" onclick="$(this).css('display', 'none'); $('#quadro_format_id').css('display', 'block');"><?php echo ($HttpContentResult->product_model->getQuadroFormatName())?$HttpContentResult->product_model->getQuadroFormatName():"escolher"; ?></div>
													<select id="quadro_format_id" name="quadro_format_id" style="display: none;">
														<?php 
														echo showOptionsAndSelectTheSelectableItem($retornoDaPaginaHTML->array_format, $ProductModel->quadro_format_id);
														?>
													</select>
								</div>
								<br />
								<div>
									<label>Estilo:</label>
													<div class="ready" id="quadro_style_button_show" onclick="$(this).css('display', 'none'); $('#quadro_style_id').css('display', 'block');"><?php echo ($HttpContentResult->product_model->getQuadroStyleName())?$HttpContentResult->product_model->getQuadroStyleName():"escolher"; ?></div>
													<select id="quadro_style_id" name="quadro_style_id" style="display: none;">
														<?php
														echo showOptionsAndSelectTheSelectableItem($retornoDaPaginaHTML->array_style, $ProductModel->quadro_style_id);
														?>
													</select>
								</div>
								<br />
								<div>
									<label for="year">Ano:</label><input  type="text"  name="year" id="year" value="<?php echo $HttpContentResult->product_model->year; ?>" />
								</div>
								<br />
								<div>
									<label for="year">Altura do original:</label><input  type="text"  name="quadro_height" id="quadro_height" value="<?php echo str_replace(".", ",", $HttpContentResult->product_model->quadro_height); ?>" />
								</div>
								<br />
								<div>
									<label for="year">Largura do original:</label><input  type="text"  name="quadro_widht" id="quadro_widht" value="<?php echo str_replace(".", ",", $HttpContentResult->product_model->quadro_width);  ?>" />
								</div>
								<br />
								<br />
								<br />
							<?php
							/*
							 * [color] => Array([0] => stdClass Object(
			                    [image] => Array(
			                            [0] => stdClass Object(
			                                    [active] => 1
			                                    [name] => orange
			                                    [description] => descricao
			                                    [type] => jpg
			                                    [url] => assetsimgcolororange.gif
			                                    [order] => 1
			                                    [author] => autor
			                                    [locale] => 
			                                    [id] => 5
			                                    [table] => image
			                                    [table_id] => 5
			                                    [unique_paremether_by_value] => 
			                                )
			                        )
			                    [count_product_model] => 0
			                    [name] => laranja
			                    [order] => 1
			                    [id] => 5
			                    [active] => 1
			                    [table] => color
			                    [table_id] => 5
			                    [unique_paremether_by_value] => 
			                )
							 */
							function corSelecionada($p_id, $p_array_cores_selecionadas){
								foreach($p_array_cores_selecionadas as $corSelecionada){
									if($corSelecionada->id == $p_id){
										return TRUE;
									}
								}
								return FALSE;
							}
							//essas cores estão repetidas do banco, fazendo o tratamento anti repetição
							$tempCoresSelecionadasSemRepeticao = array();
							
							if(isset($retornoDaPaginaHTML->array_color_std_obj)){
								foreach($ProductModel->array_color_std_obj as $colorSelectedVO){
									$tempCoresSelecionadasSemRepeticao[$colorSelectedVO->id] = $colorSelectedVO;
								}
							}
							//listando as cores que existia antes do usuário modificar - em hidden, para facilitar "trocar" as cores
							foreach($tempCoresSelecionadasSemRepeticao as $colorSelectedVO){
								?>
								<input type="hidden" name="product_old_selected_colors[]" id="product_old_color_<?php echo $colorSelectedVO->id;?>" value="<?php echo $colorSelectedVO->id;?>" />
								<?php
							}
							//CORES - COLOR - array_color
							echo "<div class='cores'><h1>Selecione a(s) cor(es) do quadro:</h1>";
							foreach($retornoDaPaginaHTML->array_color as $colorStdVO){
//								Debug::print_r($ProductModel);
								$checked = corSelecionada($colorStdVO->id, $ProductModel->array_color_std_obj)?" checked=\"checked\" ":"";
								?>
								<label class="check">
									<input type="checkbox" name="product_color[]" id="product_color_<?php echo $colorStdVO->id;?>" <?php echo $checked;?> value="<?php echo $colorStdVO->id;?>" /><?php echo $colorStdVO->name;?>
								</label>
								<?php 
							}
							?>
							</div>
						</div>		
				</div>
				
				<?php 
				if(count($HttpContentResult->arrayProducts) > 0){
					?>
					<div class="tabelaPrecos">
						<table class="tablesorter">
							<thead>
							    <tr>
							    	<th>ID interno</th><!-- 1 -->
							    	<th>Código sku</th><!-- 2 -->
							        <th class="material">Material</th><!-- 3 -->
							        <th>Altura</th><!-- 4 -->
							        <th>Largura</th><!-- 5 -->
							        <th>Preço</th><!-- 6 -->
							        <th>Detalhes</th><!-- 7 -->
							        <th>EXCLUIR</th><!-- 8 -->
							    </tr>
						    </thead>
						    <tbody>
						    <?php
						     	foreach($HttpContentResult->arrayProducts as $product): 
						     		if($product->active >= 0){
						    			$div_id = "p_div_".$product->id;
						    		/*
						    		 * [0] => stdClass Object(
									            [product_democrart_quadro_id] => 440
									            [material_id] => 1
									            [material_name] => Papel algod�o
									            [width] => 51.15
									            [height] => 73.15
									            [product_model_name] => Living in Europe
									            [product_model_id] => 144
									            [name] => Living in Europe
									            [description] => 
									            [price] => 1288.3400
									            [sku] => 35173109-977-144
									            [stock] => 180
									            [drawing] => 180
									            [order] => 1
									            [view] => 
									            [hit] => 
									            [date_insert] => 00-00-0000 00:00:00
									            [date_update] => 00-00-0000 00:00:00
									            [base_price] => 
									            [id] => 440
									            [active] => 1
									            [table] => product_democrart_quadro_id
									            [table_id] => 440
									            [unique_paremether_by_value] => 
									        )
						    		 */
						    	?>
						    <tr id="tr_<?php echo $product->id;?>">
						    	<td><?php echo $product->id;?>
						    		<input id="<?php echo $div_id."_id";?>" type="hidden" name="product_id[]" value="<?php echo $product->id;?>" ></input>
						    	</td>
						    	<td><input id="<?php echo $div_id."_sku";?>" type="text" name="product_sku[]" value="<?php echo $product->sku;?>"></input></td>
						    	<td class="material">
									<div class="ready" id="quadro_<?php echo $div_id;?>_button_show" onclick="$(this).css('display', 'none'); $('#select_<?php echo $div_id;?>').css('display', 'block');"><?php echo $product->material_name; ?></div>
						    		<select id="select_<?php echo $div_id;?>" name="material_id[]" style="display: none;">
										<?php 
										echo showOptionsAndSelectTheSelectableItem($retornoDaPaginaHTML->array_material, $product->material_id);
										?>
									</select>
						    	</td>
						    	<td>
						    		<input id="<?php echo $div_id."_height";?>" type="text" name="product_height[]" value="<?php echo str_replace(".", ",", $product->height);?>" ></input>
						    	</td>
						    	<td>
						    		<input id="<?php echo $div_id."_width";?>" type="text" name="product_width[]" value="<?php echo str_replace(".", ",", $product->width);?>" ></input>
						    	</td>
						    	<td>
						    		<input id="<?php echo $div_id."_price";?>" type="text" name="product_price[]" value="<?php echo DataHandler::convertMoneyToBrazil($product->price, FALSE);?>" ></input>
						    	</td>
						    	<td class="ico"><a target="_blank" href="<?php echo Config::getRootPath("admin/product/quadro/tamanho/editar/id.".$product->id."/product_model.".$product_model_id);?>"><img src="<?php echo Config::getAsset("assets/img/visualizar.png")?>" alt="Ver detalhes" title="Ver detalhes" /></a></td>
						    	<?php if($product->active > 0){?>
							    	<td class="ico"><a href="javascript:void(0);" id="<?php echo $product->id;?>" class="notDeleteItem">Excluir</a></td>
						    	<?php }else{?>
							    	<td class="ico"><a href="javascript:void(0);" id="<?php echo $product->id;?>" class="deleteItem">Excluir</a></td>
						    	<?php }?>
						    </tr>
						    <?php }//end if active?>
						    <?php endforeach;?>
						 </tbody>  
						</table>
					</div>
				<?php 
				}//end if(count($HttpContentResult->arrayProducts) > 0){
				?>
				<?php if($ProductModel->id >0){?>
					<div id="novo_tamanho">
						<input type="button" class="btn" value="Cadastrar Novo Tamanho" onclick="window.open('<?php echo Config::getRootPath("admin/product/quadro/tamanho/novo/product_model.".$HttpContentResult->product_model->id."/product_model_id.".$HttpContentResult->product_model->product_model_id); ?>', '_top');"></input>
					</div>
				<?php }?>
				<div class="obs">
						<fieldset>
							<legend>Sobre a obra:</legend>
							<textarea id="description" name="description"><?php echo $HttpContentResult->product_model->description; ?></textarea>
						</fieldset>
						
						



				<?php 
				
				$ProductModelVO = $HttpContentResult->product_model;
				if(FALSE){
					$ProductModelVO = new ProductModelQuadroVO();
				}
				$arrayLinkVO_products = $ProductModelVO->getLinks("product_model_democrart_quadro", 1);
				
				//Debug::print_r($arrayLinkVO_products);
				
				if(count($arrayLinkVO_products) > 0){
					?>
					<h2>Produtos relacionados:</h2><br />
					<div class="tabelaPrecos">
						<table class="tablesorter">
							<thead>
							    <tr>
							    	<th>ID do quadro</th>
							    	<th>img</th>
							    	<th>Nome</th>
							        <th>Artista</th>
							        <th>Detalhes</th>
							        <th>Remover Vinculo</th>
							    </tr>
						    </thead>
						    <tbody>
						    <?php
								
						     	foreach($arrayLinkVO_products as $LinkVO):
									if(FALSE){
										$LinkVO = new LinkVO();
									}
									//Debug::print_r($LinkVO);exit();	
						     		$ReturnResultVO = $LinkVO->getLinkedVO();
						     		
						     		if($ReturnResultVO->success){
						     			$product = $ReturnResultVO->result;
						     			$product->id = $product->product_model_democrart_quadro_id;
						     			
						    			$div_id = "p_div_link_".$product->id;
						    		
						    	?>
						    <tr id="tr_link_<?php echo $product->id;?>">
						    	<td><?php echo $product->id;?></td>
						    	<td><img src="<?php echo Config::getRootPath();?>image/get_image/product_id.<?php echo $product->id;?>/max_width.100/max_height.100/crop.1" /></td>
						    	<td>
									<?php echo $product->name;?>
						    	</td>
						    	<td>
						    		<?php echo $product->getQuadroArtistName();?>
						    	</td>
						    	<td class="ico">
									<a target="_blank" href="<?php echo Config::getRootPath("admin/product/quadro/detail/product.".$product->id);?>"><img src="<?php echo Config::getAsset("assets/img/visualizar.png")?>" alt="Ver detalhes" title="Ver detalhes" /></a>
								</td>
						    	<td class="ico">
									<a href="javascript:void(0);" id="<?php echo $product->id;?>_<?php echo $ProductModel->id."_".$LinkVO->id;?>" class="unlinkItem">Desvincular</a>
								</td>
						    </tr>
						    <?php 
						     		}//end if//success
						    endforeach;?>
						 </tbody>  
						</table>
					</div>
				<?php 
				}//end if(count($HttpContentResult->arrayProducts) > 0){
				?>
				<fieldset>
					<legend>Adicionar produtos relacionados (insira o link completo separado por pulo de linha ou espaço):</legend>
					<textarea id="related_products" name="related_products"></textarea>
				</fieldset>




						<br />
						<input class="btn" type="submit" value="Salvar alterações" />
				  </div>
    	  
    </div>
    </form>
</div>

<?php  include Config::getFolderView("parts/admin/footer.php"); ?>

</body>
</html>
